<?php
class UserController extends Zend_Controller_Action {
	
	private $user;
	private $advertisement;
	private $message;
	private $userComment;
	private $advertisementMessage;
	private $logger;
	public function init() {
		$this->user = new User();
		$this->advertisement = new Advertisement();
		$this->advertisementMessage = new AdvertisementMessage();
		$this->message = new Message();
		$this->userComment = new UserComment();
		$this->logger = Zend_Registry::get ( "logger" );
	}
	
	public function loginAction() {
		$config = Zend_Registry::get("config");
		if (! $this->_helper->user->isLoggedIn ()) {
			$next = $this->_getParam ( "next" );
			$form = new LoginForm ( $next );
			
			$this->view->form = $form;
			$this->view->next = $next;
			if ($this->getRequest ()->isPost ()) {
				if ($form->isValid ( $_POST )) {
					$email = $form->getValue ( 'email' );
					$password = $form->getValue ( 'password' );
					
					$user = new User ();
					$auth = Zend_Auth::getInstance ();
					$authAdapter = new Zend_Auth_Adapter_DbTable ( $user->getAdapter (), 'user', 'email', 'password', 'md5(?) AND status="active"' );
					$authAdapter->setIdentity ( $email )->setCredential ( $password );
					$result = $auth->authenticate ( $authAdapter );
					if ($result->isValid ()) {
						$userObj = $authAdapter->getResultRowObject ();
						$user = $this->user->findById ( $userObj->id );
						
						SessionUtil::setProperty ( Constant::USER_DATA, $user );
						$this->user->updateLastAccess ( $user->id, $this->_helper->generator->generateCurrentTime () );
						
						$next = $form->getValue("next");
						if (empty($next)) {
							$this->_redirect($config->baseurl);
						} else {
							$this->_redirect($next);
						}
					}
				}
			}
		} else {
			$this->_redirect ( "/" );
		}
	}
	
	public function logoutAction() {
		$config = Zend_Registry::get("config");
		$storage = new Zend_Auth_Storage_Session ( );
		$storage->clear ();
		Zend_Registry::set ( Constant::USER_DATA, null );
		Zend_Session::destroy ( true, false );
		$this->_redirect ( $config->baseurl );
	}
	
	public function viewAction() {
		$config = Zend_Registry::get("config");
		if ($this->_helper->user->isLoggedIn()) {
			
		} else {
			$this->_redirect($config->baseurl."/user/login/?next=".urlencode($this->_helper->generator->getCurrentURI()));
		}
	}
	
	public function settingAction() {
		
	}
	
	/**
	 * User comment on others advertisement.
	 */
	public function addCommentAction() {
		if ($this->_helper->user->isLoggedIn()) {
			$loggedInUser = $this->_helper->user->getUserData();
			$advertisement_id = $this->_getParam("advertisement_id");
			$postUserId = $this->_getParam("user_id");
			$postUser = $this->user->findById($postUserId);
			if ($postUserId != $loggedInUser->id) {
				$this->logger->log ( "Post user is not the login user", Zend_Log::ERR );
				throw new Exception("Post user is not the login user");
			}
			$targetUser = Advertisement::getUser($advertisement_id);
			$message = $this->_getParam("comment");
			$db = Zend_Registry::get("db");
			$db->beginTransaction();
			$data = array(
				"user_id"=>$postUserId,
				"type"=>MessageType::COMMENT,
				"body"=>$message,
				"status"=>MessageStatus::ACTIVE,
				"created"=>$this->_helper->generator->generateCurrentTime(),
				"ipaddress"=>$this->_helper->generator->getIpAddress()
			);
			$messageId = $this->message->postMessage($data);
			$data = array(
				UserComment::TARGET_USER => $targetUser->id,
				UserComment::MESSAGE => $messageId
			);
			$this->userComment->addNew($data);
			$data = array(
				AdvertisementMessage::ADVERTISEMENT => $advertisement_id,
				AdvertisementMessage::MESSAGE => $messageId
			);
			$this->advertisementMessage->addEntry($data);
			$db->commit();
			
			$this->_redirect(urldecode($this->_getParam("next")));
		} else {
			$this->_redirect($config->baseurl."/user/login/?next=".urlencode($this->_helper->generator->getCurrentURI()));
		}
	}
}
?>